package com.cycling.app.utils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.cycling.app.ContextHolder;
import com.cycling.app.bean.User;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiZhanping onShrinkScreen 2016/4/5.
 */
public class DribblePrefs {

    private static volatile DribblePrefs singleton;
    private final SharedPreferences prefs;
    private long user_id;
    private String user_name;
    private String mobile;
    private String money;
    private boolean isLoggedIn = false;

    private List<DribbleLoginStatusListener> loginStatusListeners;

    public static DribblePrefs get() {
        if (singleton == null) {
            synchronized (DribblePrefs.class) {
                singleton = new DribblePrefs();
            }
        }
        return singleton;
    }

    private DribblePrefs() {
        prefs = PreferenceManager.getDefaultSharedPreferences(ContextHolder.getContext());
        User user = getUser();
        if (isLoggedIn = user != null) {
            user_id = user.getId();
            user_name = user.getUsername();
            mobile = user.getMobile();
            money = String.valueOf(user.getMoney());
        }
    }

    public long getUser_id() {
        return user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public String getMobile() {
        return mobile;
    }

    public String getMoney() {
        return money;
    }

    public User getUser() {
        User user = new Gson().fromJson(prefs.getString("user", null), User.class);
        return user == null ? null : user;
    }

    public boolean isLoggedIn() {
        return isLoggedIn;
    }

    public boolean login(User user) {
        if (isLoggedIn = user != null) {
            prefs.edit().putString("user", new Gson().toJson(user)).apply();
            user_id = user.getId();
            user_name = user.getUsername();
            mobile = user.getMobile();
            money = String.valueOf(user.getMoney());
            dispatchLoginEvent();
        }
        return isLoggedIn;
    }

    public boolean update(User user) {
        if (user != null && isLoggedIn) {
            prefs.edit().putString("user", new Gson().toJson(user)).apply();
            user_id = user.getId();
            user_name = user.getUsername();
            mobile = user.getMobile();
            money = String.valueOf(user.getMoney());
            return true;
        }
        return false;
    }

    public void logout() {
        prefs.edit().putString("user", null).apply();
        user_id = -1L;
        user_name = "";
        mobile = "";
        money = String.valueOf(0.00d);
        isLoggedIn = false;
        dispatchLogoutEvent();
    }

    public interface DribbleLoginStatusListener {
        void onDribbleLogin();

        void onDribbleLogout();
    }


    public void addLoginStatusListener(DribbleLoginStatusListener listener) {
        if (loginStatusListeners == null) {
            loginStatusListeners = new ArrayList<>();
        }
        loginStatusListeners.add(listener);
    }

    public void removeLoginStatusListener(DribbleLoginStatusListener listener) {
        if (loginStatusListeners != null) {
            loginStatusListeners.remove(listener);
        }
    }

    private void dispatchLoginEvent() {
        if (loginStatusListeners != null && !loginStatusListeners.isEmpty()) {
            for (DribbleLoginStatusListener listener : loginStatusListeners) {
                listener.onDribbleLogin();
            }
        }
    }

    private void dispatchLogoutEvent() {
        if (loginStatusListeners != null && !loginStatusListeners.isEmpty()) {
            for (DribbleLoginStatusListener listener : loginStatusListeners) {
                listener.onDribbleLogout();
            }
        }
    }
}
